package com.wdd.libbase.mvp

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.uber.autodispose.AutoDisposeConverter
import com.wdd.libbase.util.RxLifecycleUtils

/**
 *
 * @ProjectName:    OwnTool
 * @Package:        com.wdd.libbase.mvp
 * @ClassName:      BasePresenter
 * @Description:     java类作用描述
 * @Author:         王丹丹
 * @CreateDate:     2019/12/9 11:12
 * @UpdateUser:     更新者
 * @UpdateDate:     2019/12/9 11:12
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
open class BasePresenter<V : IView, M : IModel> constructor(var mRootView: V?, var mModel: M?) :
    IPresenter {

    private var lifecycleOwner: LifecycleOwner? = null

    override fun setLifecycleOwner(lifecycleOwner: LifecycleOwner?) {
        this.lifecycleOwner = lifecycleOwner
    }


    /**
     * 绑定周期，给rxjava用的
     */
    fun <T> bindLifecycle(): AutoDisposeConverter<T> {
        if (null == lifecycleOwner) {
            throw NullPointerException("lifecycleOwner == null")
        }
        return RxLifecycleUtils.bindLifecycle(lifecycleOwner)
    }


    override fun onCreate(owner: LifecycleOwner?) {
    }

    override fun onStart(owner: LifecycleOwner?) {
    }

    override fun onResume(owner: LifecycleOwner?) {
    }

    override fun onPause(owner: LifecycleOwner?) {
    }

    override fun onStop(owner: LifecycleOwner?) {
    }


    override fun onLifecycleChanged(owner: LifecycleOwner?, event: Lifecycle.Event?) {
    }

    override fun onDestroy(owner: LifecycleOwner?) {
        mModel?.onDestroy()
        mModel = null
        mRootView = null
    }
}